package com.business.web.spring;


import java.util.List;
import org.apache.log4j.Logger;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.business.domain.T_task_node_instance;
import com.easyworkflow.web.spring.WorkFlowController;

//import com.workflow.WorkFlowUtils;
import com.common.db.JsonVO;
import com.common.db.Pg;
import com.common.utils.BeanUtils;
import com.business.domain.T_company;
//import com.business.mapper.T_companyMapper;
import com.business.service.T_companyService;

@Controller
@RequestMapping("/t_company")

public class T_companyController extends WorkFlowController{

	private static Logger log = Logger.getLogger(T_companyController.class.getName());

	@Resource
    private T_companyService t_companyService;

	//@Autowired
	//private WorkFlowUtils workFlowUtils;

	//@Resource
	//private T_companyMapper mapper;
	//private int id;
	//private int[] ids;
	private Pg pg;

	/**
	 * 1、获取 T_company信息列表，并且在列表页面展示
	 * @param request
	 * @return Pg，json格式
	 */
	@RequestMapping("/getAllT_company")
	@ResponseBody
	public Pg getAllT_company(HttpServletRequest request){
        int pageSize = 10;
        int startIndex = 0;
        T_company t_company = new T_company();
        try{
	        if(request.getParameter("pageSize")!=null){
	        	pageSize = BeanUtils.stringToInt("pageSizeFlag", request.getParameter("pageSize"));
	        }
	        if(request.getParameter("startIndex")!=null){
	            startIndex = BeanUtils.stringToInt("startIndexFlag", request.getParameter("startIndex"));
	        }
	        if(request.getParameter("process_code")!=null){ //取得动作流 流程英文名称，后台查询数据关联使用
	        	t_company.setProcess_code(request.getParameter("process_code"));
	        }
		 //if(request.getParameter("company_id")!=null&&!"".equals(request.getParameter("company_id"))){
	     //   	t_company.setCompany_id(new Long(request.getParameter("company_id")));
	     //}
			if(request.getParameter("company_name")!=null&&!"".equals(request.getParameter("company_name"))){
	        	t_company.setCompany_name(request.getParameter("company_name"));
	        }
			if(request.getParameter("company_addres")!=null&&!"".equals(request.getParameter("company_addres"))){
	        	t_company.setCompany_addres(request.getParameter("company_addres"));
	        }
			if(request.getParameter("company_tel")!=null&&!"".equals(request.getParameter("company_tel"))){
	        	t_company.setCompany_tel(request.getParameter("company_tel"));
	        }
			if(request.getParameter("company_linkman")!=null&&!"".equals(request.getParameter("company_linkman"))){
	        	t_company.setCompany_linkman(request.getParameter("company_linkman"));
	        }
			if(request.getParameter("ext1")!=null&&!"".equals(request.getParameter("ext1"))){
	        	t_company.setExt1(request.getParameter("ext1"));
	        }
			if(request.getParameter("ext2")!=null&&!"".equals(request.getParameter("ext2"))){
	        	t_company.setExt2(request.getParameter("ext2"));
	        }
			if(request.getParameter("ext3")!=null&&!"".equals(request.getParameter("ext3"))){
	        	t_company.setExt3(request.getParameter("ext3"));
	        }
			if(request.getParameter("inserttime")!=null&&!"".equals(request.getParameter("inserttime"))){
	        	t_company.setInserttime(request.getParameter("inserttime"));
	        }
			if(request.getParameter("updatetime")!=null&&!"".equals(request.getParameter("updatetime"))){
	        	t_company.setUpdatetime(request.getParameter("updatetime"));
	        }
			if(request.getParameter("creater")!=null&&!"".equals(request.getParameter("creater"))){
	        	t_company.setCreater(request.getParameter("creater"));
	        }
			if(request.getParameter("updater")!=null&&!"".equals(request.getParameter("updater"))){
	        	t_company.setUpdater(request.getParameter("updater"));
	        }
			if(request.getParameter("status")!=null&&!"".equals(request.getParameter("status"))){
	        	t_company.setStatus(request.getParameter("status"));
	        }
			if(request.getParameter("memo")!=null&&!"".equals(request.getParameter("memo"))){
	        	t_company.setMemo(request.getParameter("memo"));
	        }
			if(request.getParameter("img")!=null&&!"".equals(request.getParameter("img"))){
	        	t_company.setImg(request.getParameter("img"));
	        }
			if(request.getParameter("company_no")!=null&&!"".equals(request.getParameter("company_no"))){
	        	t_company.setCompany_no(request.getParameter("company_no"));
	        }
			if(request.getParameter("biztype")!=null&&!"".equals(request.getParameter("biztype"))){
	        	t_company.setBiztype(request.getParameter("biztype"));
	        }
			if(request.getParameter("province")!=null&&!"".equals(request.getParameter("province"))){
	        	t_company.setProvince(request.getParameter("province"));
	        }
			if(request.getParameter("city")!=null&&!"".equals(request.getParameter("city"))){
	        	t_company.setCity(request.getParameter("city"));
	        }
			if(request.getParameter("area")!=null&&!"".equals(request.getParameter("area"))){
	        	t_company.setArea(request.getParameter("area"));
	        }
			if(request.getParameter("channel")!=null&&!"".equals(request.getParameter("channel"))){
	        	t_company.setChannel(request.getParameter("channel"));
	        }
			if(request.getParameter("needarea1")!=null&&!"".equals(request.getParameter("needarea1"))){
	        	t_company.setNeedarea1(request.getParameter("needarea1"));
	        }
			if(request.getParameter("needarea2")!=null&&!"".equals(request.getParameter("needarea2"))){
	        	t_company.setNeedarea2(request.getParameter("needarea2"));
	        }
			if(request.getParameter("channellinkman")!=null&&!"".equals(request.getParameter("channellinkman"))){
	        	t_company.setChannellinkman(request.getParameter("channellinkman"));
	        }
			if(request.getParameter("needbuildingno")!=null&&!"".equals(request.getParameter("needbuildingno"))){
	        	t_company.setNeedbuildingno(request.getParameter("needbuildingno"));
	        }
			if(request.getParameter("needhouseno")!=null&&!"".equals(request.getParameter("needhouseno"))){
	        	t_company.setNeedhouseno(request.getParameter("needhouseno"));
	        }
			if(request.getParameter("ext4")!=null&&!"".equals(request.getParameter("ext4"))){
	        	t_company.setExt4(request.getParameter("ext4"));
	        }
			if(request.getParameter("ext5")!=null&&!"".equals(request.getParameter("ext5"))){
	        	t_company.setExt5(request.getParameter("ext5"));
	        }
			if(request.getParameter("companytype")!=null&&!"".equals(request.getParameter("companytype"))){
	        	t_company.setCompanytype(request.getParameter("companytype"));
	        }
			if(request.getParameter("simplename")!=null&&!"".equals(request.getParameter("simplename"))){
	        	t_company.setSimplename(request.getParameter("simplename"));
	        }
			if(request.getParameter("ownername")!=null&&!"".equals(request.getParameter("ownername"))){
	        	t_company.setOwnername(request.getParameter("ownername"));
	        }
			if(request.getParameter("bussinestype")!=null&&!"".equals(request.getParameter("bussinestype"))){
	        	t_company.setBussinestype(request.getParameter("bussinestype"));
	        }
			if(request.getParameter("level")!=null&&!"".equals(request.getParameter("level"))){
	        	t_company.setLevel(request.getParameter("level"));
	        }
			if(request.getParameter("info")!=null&&!"".equals(request.getParameter("info"))){
	        	t_company.setInfo(request.getParameter("info"));
	        }
			if(request.getParameter("entertime")!=null&&!"".equals(request.getParameter("entertime"))){
	        	t_company.setEntertime(request.getParameter("entertime"));
	        }
			if(request.getParameter("lable")!=null&&!"".equals(request.getParameter("lable"))){
	        	t_company.setLable(request.getParameter("lable"));
	        }
			if(request.getParameter("logo")!=null&&!"".equals(request.getParameter("logo"))){
	        	t_company.setLogo(request.getParameter("logo"));
	        }
			if(request.getParameter("personcard1")!=null&&!"".equals(request.getParameter("personcard1"))){
	        	t_company.setPersoncard1(request.getParameter("personcard1"));
	        }
			if(request.getParameter("personcard2")!=null&&!"".equals(request.getParameter("personcard2"))){
	        	t_company.setPersoncard2(request.getParameter("personcard2"));
	        }
			if(request.getParameter("controllername")!=null&&!"".equals(request.getParameter("controllername"))){
	        	t_company.setControllername(request.getParameter("controllername"));
	        }
			if(request.getParameter("controllertel")!=null&&!"".equals(request.getParameter("controllertel"))){
	        	t_company.setControllertel(request.getParameter("controllertel"));
	        }
			if(request.getParameter("controllermail")!=null&&!"".equals(request.getParameter("controllermail"))){
	        	t_company.setControllermail(request.getParameter("controllermail"));
	        }
			if(request.getParameter("controllerjob")!=null&&!"".equals(request.getParameter("controllerjob"))){
	        	t_company.setControllerjob(request.getParameter("controllerjob"));
	        }
			if(request.getParameter("imagepath")!=null&&!"".equals(request.getParameter("imagepath"))){
	        	t_company.setImagepath(request.getParameter("imagepath"));
	        }
      	    if(request.getParameter("company_id")!=null&&!"".equals(request.getParameter("company_id"))){
	        	t_company.setCompany_id(new Integer(request.getParameter("company_id")));
	        }

	        if(request.getParameter("startdate")!=null&&!"".equals(request.getParameter("startdate"))){
	        	t_company.setStartdate(request.getParameter("startdate"));
	        }
	        if(request.getParameter("enddate")!=null&&!"".equals(request.getParameter("enddate"))){
	        	t_company.setEnddate(request.getParameter("enddate"));
	        }
	        t_company.setStartNo( startIndex );
	        t_company.setPageSize( pageSize );
	        t_company.setEndNo( pageSize );
	    	List<T_company> findAll = t_companyService.findAll(t_company);
	        int totalCount = t_companyService.findCountAll(t_company);
	       	pg = new Pg(findAll, totalCount, pageSize, startIndex);
	       	request.setAttribute("t_companyList", findAll);
		    request.setAttribute("pg", pg);
	    } catch (Exception e) {
			e.printStackTrace();
			log.error("------query t_companyList error:"+e);
		}

		return pg;
	}

	/**
	 * 2、新增保存  T_company信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param t_company
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveT_company")
	@ResponseBody
	public JsonVO saveT_company(T_company t_company, HttpServletRequest request,HttpServletResponse response){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		//如果有特殊需求，需要将json对象值转成java实体对象属性值
		if(t_companyService.save(t_company)>0){
	        vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 3。1、修改T_company信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param t_company
	 * @param request
	 * @return JsonVO
	 */
	@RequestMapping("/updateT_company")
	@ResponseBody
	public JsonVO updateT_company(T_company t_company, HttpServletRequest request){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		//如果有特殊值需要处理，在此处手工转换
		//T_company t_companyTemp = t_companyService.findById(t_company);
		//t_companyTemp.setXXX(t_company.getXXX());
		//if(t_companyService.update(t_companyTemp)){

		if(t_companyService.update(t_company)>0){
	        vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

    /**
     * 3。2、提交审核业务数据
     * 修改T_company信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     * @param t_company
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/tjshT_company")
    @ResponseBody
    public JsonVO tjshT_company(T_company t_company, HttpServletRequest request) {
        //启动工作流； 请假流程；
        return startMyTask( getTaskIdByBiztype(t_company.getBiztype()),t_company.getBiztype(), t_company.getCompany_id(), request);
    }

    /**
     * 3.3、提交T_company审核通过，并且返回成功或者失败信息json格式结果如：result:[success/error]
     * @param t_task_node_instance
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/shtgT_company")
    @ResponseBody
    public JsonVO shtgT_company(T_task_node_instance t_task_node_instance, HttpServletRequest request) {
        return doMyAgree(t_task_node_instance.getTninstanceid(), t_task_node_instance.getNote(), request);
    }

    /**
     * 3.4、提交T_company审核退回，信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     * @param t_task_node_instance
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/shthT_company")
    @ResponseBody
    public JsonVO shthT_company(T_task_node_instance t_task_node_instance, HttpServletRequest request) {
        return doMyBack(t_task_node_instance.getTninstanceid(), t_task_node_instance.getNote(), request);
    }
    
	/**
	 * 4、根据id查询T_company单个信息并显示在修改页面
	 * @param id
	 * @param request
	 * @return T_company json格式字符串
	 */
	@RequestMapping("/getT_company")
	@ResponseBody
	public T_company getT_company(Integer id,HttpServletRequest request){
		T_company t_company = new T_company();
		t_company.setCompany_id(id);
		return t_companyService.findById(t_company);
	}

	@RequestMapping("/delT_companyAllFile")
	@ResponseBody
	public JsonVO delT_companyAllFile(Integer id,HttpServletRequest request,HttpServletResponse response){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);

		T_company t_company = new T_company();
		t_company.setCompany_id(id);
		t_company = t_companyService.findById(t_company);
		t_company.setImagepath("");
		t_company.setImg("");

		if(t_companyService.update(t_company)>0){
			vo.setResult("success");
			vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 5、删除T_company根据id
	 * @param id
	 * @param request
	 * @param response
	 * return JsonVO ：json格式字符串
	 */
	@RequestMapping("/delT_company")
	@ResponseBody
	public JsonVO delT_company(Integer id,HttpServletRequest request,HttpServletResponse response){
        JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);

		T_company t_company = new T_company();
		t_company.setCompany_id(id);

		if(t_companyService.delete(t_company)>0){
			vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 6、跳转到t_company/toT_companyList查询所有数据页面
	 *  return /WEB-INF/views/business/t_company/t_companyList.jsp
	 */
    @RequestMapping(value = "/toT_companyList")
    public String toT_companyList() {
    	log.debug("------跳转到/WEB-INF/views/business/t_company/t_companyList.jsp------");
        return "business/t_company/t_companyList";
    }

	/**
	 * 7、新增、修改、详情页面t_company/toT_companyCurd跳转
     * business/t_company/T_companyCurd.jsp
	 */
    @RequestMapping(value = "/toT_companyCurd")
    public String toT_companyCurd() {
    	log.debug("------跳转到/WEB-INF/views/business/t_company/t_companyCurd.jsp------");
        return "business/t_company/t_companyCurd";
    }
	@RequestMapping(value = "/toT_companyCurdSH")
	public String toT_companyCurdSH() {
		log.debug("------跳转到/WEB-INF/views/business/t_company/t_companyCurdSH.jsp------");
		return "business/t_company/t_companyCurdSH";
	}
	/**
	 * 6、跳转到t_company/toT_companyOwnerList查询所有数据页面
	 *  return /WEB-INF/views/business/t_company/t_companyOwnerList.jsp
	 */
	@RequestMapping(value = "/toT_companyOwnerList")
	public String toT_companyOwnerList() {
		log.debug("------跳转到/WEB-INF/views/business/t_company/t_companyOwnerList.jsp------");
		return "business/t_company/t_companyOwnerList";
	}

	/**
	 * 7、新增、修改、详情页面t_company/toT_companyOwnerCurd 跳转
	 * business/t_company/t_companyOwnerCurd.jsp
	 */
	@RequestMapping(value = "/toT_companyOwnerCurd")
	public String toT_companyOwnerCurd() {
		log.debug("------跳转到/WEB-INF/views/business/t_company/t_companyOwnerCurd.jsp------");
		return "business/t_company/t_companyOwnerCurd";
	}

    /**
	 * 8、批量删除T_company根据主键id
	 * 是否要实现批量删除，待考虑，基本上用途不大
	 * @param id
	 * @param request
	 * @param response
	@RequestMapping("/delT_companyBatch")
	public String delT_companyBatch(HttpServletRequest request,HttpServletResponse response){
		String[] ids = request.getParameterValues("ids");
		System.out.println("------ids="+ids);
		T_company t_company = new T_company();
		int j=0;
		for(int i =0;i< ids.length;i++){
			j++;
			log.error("------ids[i]="+ids[i]);
			t_company.setCompany_id(new Integer(ids[i]).intValue());
			t_companyService.delete(t_company);
		}
		if(j==ids.length){
			log.debug("------t_company批量删除成功！------");
		}else{
			log.error("------t_company批量删除部分成功！部分失败！------");
		}

		return "redirect:/t_company/getAllT_company";
	}
*/



}









